package com.scm.controller;

import com.scm.dao.PayRecordMapper;
import com.scm.pojo.PayRecord;
import com.scm.pojo.PoItem;
import com.scm.pojo.PoMain;
import com.scm.pojo.SoMain;
import com.scm.service.PayRecordService;
import com.scm.service.PoMainService;

import com.scm.service.ScmService;
import com.scm.service.SoMainService;
import com.scm.util.Page;
import com.scm.util.QueryResult;
import com.scm.util.Result;
import com.scm.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName FinanceController
 * @Description TODO
 * @Author Xu
 * @Date 2021/7/15 17:37
 * @Version 1.0
 **/
@RestController
public class FinanceController {

    @Autowired
    private SoMainService soMainService;

    @Autowired
    private Util util;

    @Autowired
    private PoMainService poMainService;

    @Autowired
    private PayRecordService payRecordService;

    @Autowired
    private ScmService scmService;
    @RequestMapping("getSoMain1")
    public QueryResult getSoMain1(Integer currentPage, Integer pageSize){
        Map<String, Object> map = Page.get(currentPage, pageSize);
        List<SoMain> list = soMainService.getSoMain1(map);
        List<SoMain> list1 = util.showSoMain(list);
        int total = soMainService.count1();
        return QueryResult.ok(total,list1);
    }
    @RequestMapping("getSoMain2")
    public QueryResult getSoMain2(Integer currentPage, Integer pageSize){
        Map<String, Object> map = Page.get(currentPage, pageSize);
        List<SoMain> list = soMainService.getSoMain2(map);
        List<SoMain> list1 = util.showSoMain(list);
        int total = soMainService.count2();
        return QueryResult.ok(total,list1);
    }
    @RequestMapping("getSoMain3")
    public QueryResult getSoMain3(Integer currentPage, Integer pageSize){
        Map<String, Object> map = Page.get(currentPage, pageSize);
        List<SoMain> list = soMainService.getSoMain3(map);
        List<SoMain> list1 = util.showSoMain(list);
        int total = soMainService.count3();
        return QueryResult.ok(total,list1);
    }

    @RequestMapping("proceeds")
    public Result proceeds(String soID, HttpServletRequest request){
        String account = (String) request.getSession().getAttribute("account");
        SoMain soMain = soMainService.getSoMain(soID);
        String date = new SimpleDateFormat("yyyy-MM-dd hh:mm").format(new Date());
        PayRecord payRecord = new PayRecord(date,soMain.getPoTotal(),account,soID,"1");
        payRecordService.addPayRecord(payRecord);
        if (soMain.getPayType().equals("预付款发货")&&soMain.getStatus().equals("1")){
            soMainService.preProceeds(soID);
        }else {
            soMainService.proceeds(soID);
        }
        return Result.ok(null);
    }

    @RequestMapping("getPoMain1")
    public QueryResult getPoMain1(Integer currentPage, Integer pageSize){
        Map<String ,Object> map = Page.get(currentPage,pageSize);
        List<PoMain> list = poMainService.getPoMain1(map);
        List<PoMain> show = util.show(list);
        int total = poMainService.count1();
        return QueryResult.ok(total,show);
    }
    @RequestMapping("getPoMain2")
    public QueryResult getPoMain2(Integer currentPage, Integer pageSize){
        Map<String ,Object> map = Page.get(currentPage,pageSize);
        List<PoMain> list = poMainService.getPoMain2(map);
        List<PoMain> show = util.show(list);
        int total = poMainService.count2();
        return QueryResult.ok(total,show);
    }
    @RequestMapping("getPoMain3")
    public QueryResult getPoMain3(Integer currentPage, Integer pageSize){
        Map<String ,Object> map = Page.get(currentPage,pageSize);
        List<PoMain> list = poMainService.getPoMain3(map);
        List<PoMain> show = util.show(list);
        int total = poMainService.count3();
        return QueryResult.ok(total,show);
    }

    @RequestMapping("payPoMain")
    public Result payPoMain(String poID,HttpServletRequest request){
        String account = (String) request.getSession().getAttribute("account");
        PoMain poMain = poMainService.findPoMain(poID);
        String date = new SimpleDateFormat("yyyy-MM-dd hh:mm").format(new Date());
        PayRecord payRecord = new PayRecord(date,poMain.getPoTotal(),account,poID,"-1");
        payRecordService.addPayRecord(payRecord);
        if (!poMain.getPayType().equals("预付款发货")||(poMain.getPayType().equals("预付款发货")&&poMain.getStatus().equals("3"))){
            poMainService.proceeds(poID);
        }else {
            poMainService.preProceeds(poID);
        }

        return Result.ok(poMain.getPayType());
    }

    @RequestMapping("getAllPayRecord")
    public QueryResult getAllPayRecord(Integer currentPage, Integer pageSize){
        Map<String, Object> map = Page.get(currentPage, pageSize);
        List<PayRecord> all = payRecordService.getAll(map);
    all=payRecordService.showPayRecord(all);
        int total = payRecordService.getAllCount();
        return QueryResult.ok(total,all);

    }
    @RequestMapping("selectPayRecord")
    public QueryResult selectPayRecord(Integer currentPage, Integer pageSize, String pay_type, String orderCode, String time) throws ParseException {
        Map map = Page.get(currentPage,pageSize);
        String[] split = time.split("~");
        map.put("pay_type",pay_type);
        map.put("orderCode",orderCode);
        map.put("minDate",split[0].trim()+" 00:00");
        map.put("maxDate",split[1].trim()+" 00:00");
        List<PayRecord> payRecords = payRecordService.selectPayRecord(map);
        System.out.println(payRecords);
        payRecords = payRecordService.showPayRecord(payRecords);
        int total = payRecordService.countSelectPayRecord(map);
        return QueryResult.ok(total,payRecords);
    }
}
